<?php
require_once ('Controller/Mobile/Common.inc.php');
require_once ('View/Result.inc.php');
#require_once ('Model/DeviceModel.inc.php');
final class Controller extends common {
	private $Result;
	private $Loader;
	private $Callback = NULL;
	public function __construct () {
		parent::__construct();
		$trace = debug_backtrace();
		if (isset ($trace[1])) {
			$this->Loader = $trace[1]['object'];
		}
	}

	public function execude () {
		header ('Content-Type: text/xml');
		$this->Result = new Result ();
		if (array_key_exists ('action', $_REQUEST)) {
			$this->getFunction ($_REQUEST['action']);
		} else {
			$this->defaultPage ();
		}
		print $this->Result->Create ($this->Callback);
	}

	public function getFunction ($action) {
		switch ($action) {
			case 'getDeviceID';
				$this->getDeviceIDPage ();
				break;
			default:
				$this->defaultPage ();
				break;
		}
	}

	public function defaultPage () {
	}


	private $DeviceID = 0;
	public function getDeviceIDPage () {
		$this->Result->setModulename (sprintf ('M%sF%s', $this->Loader->module, __FUNCTION__));
		if (array_key_exists ('IMEI', $_REQUEST) && $_REQUEST['IMEI'] != '' && array_key_exists ('PhoneNumber', $_REQUEST) && $_REQUEST['PhoneNumber'] != '' && array_key_exists ('SimSerialNumber', $_REQUEST) && $_REQUEST['SimSerialNumber'] != '' && array_key_exists ('IMSI', $_REQUEST) && $_REQUEST['IMSI'] != '' && array_key_exists ('Manufacturer', $_REQUEST) && $_REQUEST['Manufacturer'] != '' && array_key_exists ('Model', $_REQUEST) && $_REQUEST['Model'] != '' && array_key_exists ('OS', $_REQUEST) && $_REQUEST['OS'] != '' && array_key_exists ('Version', $_REQUEST) && $_REQUEST['Version'] != '') {
/*			$DeviceModel = new DeviceModel ($this->mysql);
			$this->Result->setStatus (200);
			$this->Result->setErrorCode (0);
			if (($this->DeviceID = $DeviceModel->CheckDeviceItemExistByIMEI ($_REQUEST['IMEI'])) == NULL) {
				$this->DeviceID = $DeviceModel->AddDeviceItem ($_REQUEST['IMEI'], $_REQUEST['PhoneNumber'], $_REQUEST['SimSerialNumber'], $_REQUEST['IMSI'], $_REQUEST['Manufacturer'], $_REQUEST['Model'], $_REQUEST['OS'], $_REQUEST['Version'], $Remark = '');
			} else {
				$this->DeviceID = $this->DeviceID[$DeviceModel->idRowName];
			}*/
			$this->Result->setStatus (200);
			$this->Result->setErrorCode (0);
			if (($this->DeviceID = $this->DeviceModel->CheckDeviceItemExistByIMEI ($_REQUEST['IMEI'])) == NULL) {
				$this->DeviceID = $this->DeviceModel->AddDeviceItem ($_REQUEST['IMEI'], $_REQUEST['PhoneNumber'], $_REQUEST['SimSerialNumber'], $_REQUEST['IMSI'], $_REQUEST['Manufacturer'], $_REQUEST['Model'], $_REQUEST['OS'], $_REQUEST['Version'], $Remark = '');
			} else {
				$this->DeviceID = $this->DeviceID[$this->DeviceModel->idRowName];
			}
			$this->Callback = array ($this, 'getDeviceIDPageCallBack');
		}
	}

	public function getDeviceIDPageCallBack ($dom, $result) {
		$result->setAttribute ('DeviceID', $this->DeviceID);
	}

	public function Post () {
		$this->Result->setModulename (sprintf ('M%sF%s', $this->Loader->module, __FUNCTION__));
		if (array_key_exists ('title', $_REQUEST) && $_REQUEST['title'] != '' && array_key_exists ('description', $_REQUEST) && $_REQUEST['description'] != '') {
			$this->Result->setStatus (200);
			$this->Result->setErrorCode (0);
		} else {
			$this->Result->setErrorCode (1);
		}
	}
}
?>
